﻿using IntoCommon.Protocol.Enum;
using System;
using System.IO.Ports;
using System.Text;
using System.Threading;

namespace IntoCommon.Protocol.Instruments
{
    public class ITECH
    {
        private readonly string SetRemoteMode = "SYST:REM";
        /// <summary>
        /// 获取设备信息
        /// </summary>
        private readonly string GetInstrumentInfo = "*IDN?";
        /// <summary>
        /// 设置通道
        /// </summary>
        private readonly string SetChannel = "INST";   // INST FIRST
        /// <summary>
        /// 检查通道
        /// </summary>
        private readonly string CheckChannel = "INST?";
        /// <summary>
        /// 设置通道使能
        /// </summary>
        private readonly string EnableChannel = "CHAN:OUTP";
        /// <summary>
        /// 检查通道使能设置
        /// </summary>
        private readonly string CheckEnableChannel = "CHAN:OUTP?";
        /// <summary>
        /// 设置电压
        /// </summary>
        private readonly string SetVoltage = "VOLT";
        /// <summary>
        /// 检查设置电压
        /// </summary>
        private readonly string CheckSetVoltage = "VOLT?";
        /// <summary>
        /// 设置电流
        /// </summary>
        private readonly string SetCurrent = "CURR";
        /// <summary>
        /// 检查设置电流
        /// </summary>
        private readonly string CheckSetCurrent = "CURRENT?";

        private SerialPort serialPort = null;
        private bool isRecv = false;
        public ITECH(SerialPort serialPort)
        {
            this.serialPort = serialPort;
            this.serialPort.DataReceived += Recv;            
        }

        private bool IsRecvData()
        {
            int count = 0;
            while (!isRecv)
            {
                Thread.Sleep(100);
                ++count;
                if(count >= 50)
                {
                    return false;                    
                }
            }
            return true;
        }
        /// <summary>
        /// 启动控制
        /// </summary>
        /// <returns></returns>
        public bool Start()
        {
            if (serialPort == null) return false;
            if (!serialPort.IsOpen)
            {
                serialPort.Open();
            }
            isRecv = false;
            serialPort.WriteLine(GetInstrumentInfo);
            serialPort.WriteLine(SetRemoteMode);
            return IsRecvData();            
        }        

        private void Recv(object sender, SerialDataReceivedEventArgs e)
        {
            var data = serialPort.ReadExisting();
            isRecv = true;
        }
        public void Stop()
        {
            if (serialPort != null)
            {
                if (serialPort.IsOpen)
                {
                    serialPort.Close();
                }
            }
        }
        /// <summary>
        /// 设置通道电压值
        /// </summary>
        /// <param name="setValue"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        public bool SetChannelVoltage(double setValue, VolUnit unit)
        {
            if (serialPort.IsOpen)
            {
                isRecv = false;
                serialPort.WriteLine($"{SetVoltage} {setValue}{unit}");
                serialPort.WriteLine($"{CheckSetVoltage}");
                return IsRecvData();
            }
            return false;
        }
        /// <summary>
        /// 设置通道电流
        /// </summary>
        /// <param name="setValue"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        public bool SetChannelCurrent(double setValue, CurrUnit unit)
        {
            if (serialPort.IsOpen)
            {
                isRecv = false;
                serialPort.WriteLine($"{SetCurrent} {setValue}{unit}");
                serialPort.WriteLine($"{CheckSetCurrent}");
                return IsRecvData();
            }
            return false;
        }
        /// <summary>
        /// 选择通道
        /// </summary>
        /// <returns></returns>
        public bool ChoiceChannel(Channel channel)
        {
            if (serialPort.IsOpen)
            {
                isRecv = false;
                serialPort.WriteLine($"{SetChannel} {channel}");
                serialPort.WriteLine($"{CheckChannel}");
                return IsRecvData();               
            }
            return false;            
        }
        /// <summary>
        /// 打开通道
        /// </summary>
        /// <returns></returns>
        public bool IsEnableChannel(bool isEnable)
        {
            if (serialPort.IsOpen)
            {
                isRecv = false;
                serialPort.WriteLine($"{EnableChannel} {Convert.ToInt32(isEnable)}");                
                serialPort.WriteLine($"{CheckEnableChannel}");
                return IsRecvData();
            }
            return false;
        }
    }
}
